package com.banciyuan.bcywebview.biz.pwd;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.UserApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;

import com.banciyuan.bcywebview.utils.http.BcyPair;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.daoexample.model.PwdToken;

/**
 * Created by squall on 2016/8/29.
 */
public class FindPwdStep3Activity extends BaseActivity implements View.OnClickListener {

    private PwdToken pwdToken;
    private EditText pwd;
    private EditText pwd_comfirm;

    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_findpwd_step3);
        pwdToken = (PwdToken) getIntent().getSerializableExtra("pwdtoken");
        title = getIntent().getStringExtra(gotoUtil.INTENT_VALUE_ONE);
        findViewById(R.id.iv_cancle).setOnClickListener(this);
        findViewById(R.id.tv_auth).setOnClickListener(this);
        pwd = (EditText) findViewById(R.id.et_phone);
        pwd_comfirm = (EditText) findViewById(R.id.et_auth_code);
        if (!TextUtils.isEmpty(title)) {
            ((TextView) findViewById(R.id.find_title)).setText(title);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cancle:
                finish();
                break;
            case R.id.tv_auth:
                verfiyPwd();
                break;
        }
    }

    private void verfiyPwd() {
        String pwd_text = pwd.getText().toString();
        String pwdComfirm_text = pwd_comfirm.getText().toString();
        if (!StringUtil.notNullEqual(pwd_text, pwdComfirm_text)) {
            MyToast.show(FindPwdStep3Activity.this, getString(R.string.twice_wrong));
        } else {
            findPwd(pwd_text);
        }
    }

    private void findPwd(String pwd) {
        String url = HttpUtils.BASE_URL + UserApi.modifyPassword();
        List<BcyPair> paramsList = new ArrayList<>();
        String token = "";
        if (TextUtils.isEmpty(pwdToken.getToken())) {
            token = UserDataHelper.getInstance(this).getToken();
        } else {
            token = pwdToken.getToken();
        }
        paramsList.add(new BcyPair(HttpUtils.TOKEN, token));
        paramsList.add(new BcyPair("access_token", pwdToken.getAccess_token()));
        paramsList.add(new BcyPair(HttpUtils.REGISTER_PARAM_PASS, pwd));
        HashMap<String, String> params = HttpUtils.getData(paramsList);
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject result = new JSONObject(s);
                    MyToast.show(FindPwdStep3Activity.this, result.getString(HttpUtils.UPLOAD_DATA));
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        };
        StringRequestParam stringRequest = new StringRequestParam(Request.Method.POST, url, params, succ, error);
        VolleyQueue.getRquest(this).add(stringRequest);
    }
}
